<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="经度" prop="longitude">-->
<!--        <el-input-->
<!--          v-model="queryParams.longitude"-->
<!--          placeholder="请输入经度"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="纬度" prop="latitude">-->
<!--        <el-input-->
<!--          v-model="queryParams.latitude"-->
<!--          placeholder="请输入纬度"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="容量" prop="count">-->
<!--        <el-input-->
<!--          v-model="queryParams.count"-->
<!--          placeholder="请输入容量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="位置" prop="position">-->
<!--        <el-input-->
<!--          v-model="queryParams.position"-->
<!--          placeholder="请输入位置"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="管理员" prop="userId">-->
<!--        <el-input-->
<!--          v-model="queryParams.userId"-->
<!--          placeholder="请输入管理员"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="站点名称" prop="stationName">
        <el-input
          v-model="queryParams.stationName"
          placeholder="请输入站点名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:station:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:station:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:station:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:station:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="站点名称" align="center" prop="stationName">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleShowMap(scope.row)"
          >{{ scope.row.stationName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="容量" align="center" prop="count" />
      <el-table-column label="位置" align="center" prop="position" />
      <el-table-column label="管理员" align="center" prop="nickName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:station:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:station:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改站点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="站点名称" prop="stationName">
          <el-input v-model="form.stationName" placeholder="请输入站点名称" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
<!--          <el-input v-model="form.longitude" placeholder="请输入经度" />-->
          <el-input-number v-model="form.longitude" :precision="6" :step="0.1"></el-input-number>
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
<!--          <el-input v-model="form.latitude" placeholder="请输入纬度" />-->
          <el-input-number v-model="form.latitude" :precision="6" :step="0.1"></el-input-number>
        </el-form-item>
        <el-form-item label="容量" prop="count">
          <el-input v-model="form.count" placeholder="请输入容量" />
        </el-form-item>
        <el-form-item label="位置" prop="position">
          <el-input v-model="form.position" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="管理员" prop="userId">
<!--          <el-input v-model="form.userId" placeholder="请输入管理员" />-->
          <el-select v-model="form.userId" placeholder="请选择管理员">
            <el-option
              v-for="item in users"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-divider content-position="center">充电宝信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddPowerTreasure">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeletePowerTreasure">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="powerTreasureList" :row-class-name="rowPowerTreasureIndex" @selection-change="handlePowerTreasureSelectionChange" ref="powerTreasure">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="序列号" prop="serialNumber" width="300">
            <template slot-scope="scope">
              <el-input v-model="scope.row.serialNumber" placeholder="请输入序列号" />
            </template>
          </el-table-column>
          <el-table-column label="是否可用" prop="availableStatus">
            <template slot-scope="scope">
              <el-select v-model="scope.row.availableStatus" placeholder="请选择是否可用">
                <el-option
                  v-for="dict in dict.type.treasure_available"
                  :key="dict.value"
                  :label="dict.label"
                  :value="Number(dict.value)"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="是否出租" prop="rentalStatus" >
            <template slot-scope="scope">
              <el-select v-model="scope.row.rentalStatus" placeholder="请选择是否出租">
                <el-option
                  v-for="dict in dict.type.treasure_rental"
                  :key="dict.value"
                  :label="dict.label"
                  :value="Number(dict.value)"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <position-map @close="mapOpen=false" ref="positionMap"/>
  </div>
</template>

<script>
import { listStation, getStation, delStation, addStation, updateStation } from "@/api/system/station";
import { listUser } from '@/api/system/user'
import positionMap from "@/views/station/components/positionMap";

export default {
  name: "Station",
  dicts: ['treasure_available', 'treasure_rental'],
  components: {
    positionMap
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPowerTreasure: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 站点表格数据
      stationList: [],
      // 充电宝表格数据
      powerTreasureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        longitude: null,
        latitude: null,
        count: null,
        position: null,
        userId: null,
        stationName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      users: [],
      mapOpen: false,
    };
  },
  created() {
    this.getList();
    this.getListUser()
  },
  methods: {
    handleShowMap(row) {
      this.mapOpen = true
      this.$refs.positionMap.show(row)
    },
    getListUser() {
      listUser().then(res => {
        console.log('res:', res.rows)
        this.users = res.rows
      })
    },
    /** 查询站点列表 */
    getList() {
      this.loading = true;
      listStation(this.queryParams).then(response => {
        this.stationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        longitude: null,
        latitude: null,
        count: null,
        position: null,
        userId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        stationName: null
      };
      this.powerTreasureList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加站点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStation(id).then(response => {
        this.form = response.data;
        this.powerTreasureList = response.data.powerTreasureList;
        this.open = true;
        this.title = "修改站点";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.powerTreasureList = this.powerTreasureList;
          if (this.form.id != null) {
            updateStation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除站点编号为"' + ids + '"的数据项？').then(function() {
        return delStation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 充电宝序号 */
    rowPowerTreasureIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 充电宝添加按钮操作 */
    handleAddPowerTreasure() {
      let obj = {};
      obj.serialNumber = "";
      obj.availableStatus = "";
      obj.rentalStatus = "";
      obj.remark = "";
      this.powerTreasureList.push(obj);
    },
    /** 充电宝删除按钮操作 */
    handleDeletePowerTreasure() {
      if (this.checkedPowerTreasure.length == 0) {
        this.$modal.msgError("请先选择要删除的充电宝数据");
      } else {
        const powerTreasureList = this.powerTreasureList;
        const checkedPowerTreasure = this.checkedPowerTreasure;
        this.powerTreasureList = powerTreasureList.filter(function(item) {
          return checkedPowerTreasure.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handlePowerTreasureSelectionChange(selection) {
      this.checkedPowerTreasure = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/station/export', {
        ...this.queryParams
      }, `station_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
